package com.neu.questionnaire_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neu.questionnaire_system.beans.ResponseEntity;
import com.neu.questionnaire_system.entity.Cluster;
import com.neu.questionnaire_system.mapper.AnswerMapper;
import com.neu.questionnaire_system.mapper.ClusterMapper;
import com.neu.questionnaire_system.service.ClusterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.questionnaire_system.util.Constants;
import com.neu.questionnaire_system.util.UUIDGenerator;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xingle
 * @since 2022-10-31
 */
@Service
public class ClusterServiceImpl extends ServiceImpl<ClusterMapper, Cluster> implements ClusterService {
    @Autowired
    private ClusterMapper clusterMapperMapper;
    @Autowired
    private AnswerMapper answerMapper;

    public ResponseEntity insertCluster(String kind, String uid){

        String cid = UUIDGenerator.getOneUUID();
        int resr=clusterMapperMapper.findSameName(kind);
        ResponseEntity re = new ResponseEntity();
        if(resr>0){
            re.setMessage(Constants.ADD_FAIL_MESSAGE);
            re.setCode(Constants.FAIL_CODE);
            return re;
        }
        int res = clusterMapperMapper.insertCluster(cid, kind, uid,"0");
        re.setData(res);
        if(res > 0){
            re.setMessage(Constants.ADD_MESSAGE);
            re.setCode(Constants.SUCCESS_CODE);
        }else{
            re.setMessage(Constants.ADD_FAIL_MESSAGE);
            re.setCode(Constants.FAIL_CODE);
        }
        return re;
    }

    public ResponseEntity showCluster(){
        List<Cluster> res = clusterMapperMapper.showCluster();
        ResponseEntity re = new ResponseEntity();
        re.setData(res);
        if(res!=null){
            re.setMessage(Constants.SELECT_MESSAGE);
            re.setCode(Constants.SUCCESS_CODE);
        }else{
            re.setMessage(Constants.SELECT_FAIL_MESSAGE);
            re.setCode(Constants.FAIL_CODE);
        }
        return re;
    }

    /**
     * created by libo 查询当前用户下的群组列表
     * @param uid 传入的用户uid
     * @return 返回统一的对象
     */
    public ResponseEntity showClusterByUid(String uid){
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(Constants.SUCCESS_CODE);
        /*responseEntity.setData(clusterMapperMapper.showClusterByUid(uid));
        responseEntity.setMessage(Constants.SELECT_MESSAGE);
        return responseEntity;*/
        List<IPage<Map<String,Object>>> list = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        queryWrapper.eq("isDeleted","0");
        int current = 1;
        long count = 0;
        while (count<=clusterMapperMapper.selectCount(queryWrapper)){
            IPage<Map<String,Object>> iPage = clusterMapperMapper.showClusterByUid(new Page<>(current,Constants.PAGE_SIZE),uid);
            list.add(iPage);
            current++;
            count += iPage.getSize();
        }
        responseEntity.setData(list);
        responseEntity.setMessage(Constants.SELECT_MESSAGE);
        return responseEntity;
    }

    /**
     * created by libo 查询当前用户下的群组列表
     * @param map 传入的用户uid
     * @return 返回统一的对象
     */
    public ResponseEntity showGroupByUid(Map<String,Object> map){
        ResponseEntity responseEntity = new ResponseEntity();
        List<String> list = clusterMapperMapper.selectClusterName(map.get("uid").toString());
        responseEntity.setData(list);
        responseEntity.setCode(Constants.SUCCESS_CODE);
        responseEntity.setMessage(Constants.SELECT_MESSAGE);
        return responseEntity;
    }


    public ResponseEntity deleteCluster(String cid){

        ResponseEntity re = new ResponseEntity();

        int candelete=clusterMapperMapper.ifAnswerInCluster(cid);

        if(candelete>0){
            re.setMessage(Constants.DELETE_FAIL_MESSAGE);
            re.setCode(Constants.FAIL_CODE);
        }else {
            int res =clusterMapperMapper.updateClusterStatue("1",cid);
            if(res > 0){
                re.setMessage(Constants.DELETE_MESSAGE);
                re.setCode(Constants.SUCCESS_CODE);
            }else{
                re.setMessage(Constants.DELETE_FAIL_MESSAGE);
                re.setCode(Constants.FAIL_CODE);
            }
        }
        return re;
    }




    public ResponseEntity findClusterByKind(String kind,String uid){
        ResponseEntity re = new ResponseEntity();
        kind = "%"+kind+"%";
        List<Cluster> res = clusterMapperMapper.findClusterByKind(kind,uid);
        re.setData(res);
        if(res !=null){
            re.setMessage(Constants.SELECT_MESSAGE);
            re.setCode(Constants.SUCCESS_CODE);

        }else{
            re.setMessage(Constants.SELECT_FAIL_MESSAGE);
            re.setCode(Constants.FAIL_CODE);
        }
        return re;
    }

    /**
     * created by libo 修改群组信息
     * @param map 传入的信息
     * @return 返回统一的对象
     */
    public ResponseEntity updateCluster(Map<String,Object> map){
        /*ResponseEntity responseEntity = new ResponseEntity();
        int res = clusterMapperMapper.updateCluster(map);
        responseEntity.setCode(Constants.SUCCESS_CODE);
        responseEntity.setMessage(Constants.UPDATE_MESSAGE);
        return responseEntity;*/

        ResponseEntity re = new ResponseEntity();

        int canupdate=clusterMapperMapper.ifAnswerInCluster(map.get("cid").toString());
        if(canupdate>0){
            re.setMessage(Constants.UPDATE_FAIL_MESSAGE);
            re.setCode(Constants.FAIL_CODE);
        }else {
            int res =clusterMapperMapper.updateCluster(map);
            re.setMessage(Constants.UPDATE_MESSAGE);
            re.setCode(Constants.SUCCESS_CODE);

        }
        return re;

    }


    /**
     * created by jyh 2022-10-31
     * @param map 从前端传入的data，注意map的key要时刻保持一致性
     * @return 返回一个结果给控制层
     */

    public ResponseEntity addAnswer_Cluster(Map<String, Object> map){
        ResponseEntity responseEntity = new ResponseEntity();
        String aid=answerMapper.queryAidByAnswerName(map.get("answerName").toString());
        map.put("aid",aid);
        if(map.get("answerName") != null){
            if (clusterMapperMapper.queryExistAnswer_Cluster(map)!=0){
                responseEntity.setCode(Constants.FAIL_CODE);
                responseEntity.setMessage(Constants.USERNAME_REPEAT_MESSAGE);//表示用户已经存在不能创建
                return responseEntity;
            }
        }


        int res = clusterMapperMapper.addAnswerCluster(aid,map.get("cid").toString(),"0");
        responseEntity.setCode(Constants.SUCCESS_CODE);//封装状态码到返回的对象
        responseEntity.setMessage(Constants.ADD_MESSAGE);//封装状态消息到返回的对象
        return responseEntity;

    }
    /**
     * create by jyh用户主页展示答者信息
     *  @param map 传入的map
     * @return 返回包含答者列表信息的对象
     */
    public ResponseEntity showAnswer_Cluster(Map<String,Object> map){

        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(Constants.SUCCESS_CODE);
        responseEntity.setData(clusterMapperMapper.showAnswer_Cluster(map));
        //responseEntity.setData(answerMapper.showAnswer(map));
        responseEntity.setMessage(Constants.SELECT_MESSAGE);
        return responseEntity;
    }
    /**
     * created by jyh 删除答者
     * @param map 被删除答者名
     * @return 返回统一的封装对象
     */
    public ResponseEntity deleteAnswer_Cluster(Map<String,Object> map){
        ResponseEntity responseEntity = new ResponseEntity();

        //String aid=answerMapper.queryAidByAnswerName(map.get("answerName").toString());
        int res = clusterMapperMapper.deleteAnswer_Cluster(map);
        if (res <= 0){
            responseEntity.setCode(Constants.FAIL_CODE);//删除失败
            responseEntity.setMessage(Constants.DELETE_FAIL_MESSAGE);
            return responseEntity;
        }
        responseEntity.setCode(Constants.SUCCESS_CODE);
        responseEntity.setMessage(Constants.DELETE_MESSAGE);
        return responseEntity;
    }
    /**
     * created by jyh 根据答者名查询答者
     * @param map 传入的map
     * @return 统一的返回对象
     */
    public ResponseEntity findAidByAnswerName(Map<String,Object> map){
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(Constants.SUCCESS_CODE);
        responseEntity.setData(clusterMapperMapper.findAnswer_ClusterByAnswerName(map));
        responseEntity.setMessage(Constants.SELECT_MESSAGE);
        return responseEntity;
    }

   /**
    * created by jyh 群组里没有的答者
     * @param map 传入的map
     * @return 统一的返回对象
     */
   public ResponseEntity shownoAnswer_Cluster(Map<String,Object> map){
       ResponseEntity responseEntity = new ResponseEntity();
       responseEntity.setCode(Constants.SUCCESS_CODE);
       responseEntity.setData(clusterMapperMapper.showAnswer_Cluster(map));
       //responseEntity.setData(answerMapper.showAnswer(map));
       responseEntity.setMessage(Constants.SELECT_MESSAGE);
       return responseEntity;
   }


}
